package com.gexiaobao.pigeon.ui.adapter

import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.ReasonListBean
import com.gexiaobao.pigeon.databinding.ItemReasonListBinding

/**
 * @Author      : hxw
 * @Date        : 2023/7/28 14:04
 * @Describe    :
 */
class ReturnGoodsReasonAdapter(data: ArrayList<ReasonListBean>) :
    BaseQuickAdapter<ReasonListBean, DataBindBaseViewHolder>(R.layout.item_reason_list, data) {

    private var checkPos = -1
    private var mOnItemClickListener: OnItemCheckClickListener? = null

    override fun convert(holder: DataBindBaseViewHolder, item: ReasonListBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemReasonListBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        val reason = data[getItemPosition(item)]
        if (reason.isChecked) {
            holder.setImageResource(R.id.ivChecked, R.mipmap.icon_reason_checked)
        } else {
            holder.setImageResource(R.id.ivChecked, R.mipmap.icon_reason_unchecked)
        }

        holder.itemView.setOnClickListener {
            if (checkPos != holder.layoutPosition) {
                //取消上一个位置的勾选状态
                if (checkPos != -1) {
                    data[checkPos].isChecked = false
                    notifyItemChanged(checkPos)
                }
                //设置新的勾选状态
                checkPos = holder.adapterPosition
                data[checkPos].isChecked = true
                notifyItemChanged(checkPos)
            }
            mOnItemClickListener?.onItemClick(checkPos)
        }
    }

    fun setOnItemCheckClickListener(listener: OnItemCheckClickListener?) {
        mOnItemClickListener = listener
    }

    interface OnItemCheckClickListener {
        fun onItemClick(position: Int)
    }
}